package com.face.server.controller;

import com.face.RespMsg;
import com.face.server.service.AffectDateService;
import com.face.server.service.CSVService;
import com.face.server.vo.AffectDate;
import com.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.ParseException;
import java.util.Date;

@RestController
@RequestMapping("date")
public class AffectDateController {

    @Autowired
    AffectDateService service;

    @Autowired
    CSVService csvService;

    @GetMapping
    RespMsg groupBy(@PageableDefault(sort = "className") Pageable pageable,
                    @RequestParam(defaultValue = "9999-99-99") String end,
                    @RequestParam(defaultValue = "0000-00-00") String begin,
                    @RequestParam(defaultValue = "") String input) throws ParseException {

        return service.groupBy(input, DateUtils.parse(begin), DateUtils.parse(end), pageable);
    }


    @PostMapping
    RespMsg save(@RequestBody AffectDate affectDate) throws ParseException {
        return service.save(affectDate);
    }

    @DeleteMapping
    RespMsg delete(String[] id) throws ParseException {
        return service.delete(id);
    }


    @PutMapping
    RespMsg importFile(MultipartFile file) throws IOException {
        return file == null ?
                RespMsg.error("对不起，文件不能为空") :
                 csvService.saveAffectDate(file);
    }
}
